package com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.entity.vo.SmsUseMoneyApplicationAllowVO;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.entity.vo.SmsUseMoneyApplicationExportVO;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.entity.vo.SmsUseMoneyApplicationVO;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.entity.vo.SmsUseMoneyCancelVO;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.service.SmsUseMoneyApplicationService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-05-14
 */
@RestController
@RequestMapping("/useMoneyApplication/smsUseMoneyApplication")
@SecretBody
public class SmsUseMoneyApplicationController {
    @Autowired
    SmsUseMoneyApplicationService smsUseMoneyApplicationService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("新增用款申请")
    @PostMapping("/insert")
    public ResponseResult smsUseMoneyApplicationAdd(@RequestBody SmsUseMoneyApplicationVO smsUseMoneyApplication,
                                                    HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUseMoneyApplicationService.smsUseMoneyApplicationAdd(smsUseMoneyApplication,user_id);
    }

    @ApiModelProperty("撤销用款申请")
    @PostMapping("/cancel")
    public ResponseResult smsUseMoneyApplicationCancel(@RequestBody SmsUseMoneyCancelVO smsUseMoneyCancel,
                                                                    HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUseMoneyApplicationService.smsUseMoneyApplicationCancel(smsUseMoneyCancel,user_id);
    }

    @ApiModelProperty("审批")
    @PostMapping("allow")
    public ResponseResult smsUseMoneyApplicationAllow(@RequestBody SmsUseMoneyApplicationAllowVO allowVO,
                                                      HttpServletRequest request){
    String user_id = jwtTokenUtil.getUserIdByRequest(request);
    return smsUseMoneyApplicationService.smsUseMoneyApplicationAllow(allowVO,user_id);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getUseMoneyApplicationList(
                                                @RequestParam(value = "search_str",required = false) String search_str,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "application_type") Integer application_type,
                                                HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUseMoneyApplicationService.getUseMoneyApplicationList(search_str,user_id,application_type,pageNum,pageSize);
    }

    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getUseMoneyApplicationDetail(@RequestParam(value = "application_id") String application_id,
                                                  HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUseMoneyApplicationService.getUseMoneyApplicationDetail(application_id,user_id);
    }

    @ApiModelProperty(value = "申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getUseMoneyApplicationOriginSettingProcess() {
        return smsUseMoneyApplicationService.getUseMoneyApplicationOriginSettingProcess();
    }

    @ApiModelProperty(value = "文件下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id,
                                        HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsUseMoneyApplicationService.tableDownload(apply_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
    @ApiModelProperty(value = "查询所有申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllUseMoneyApply(
            @RequestParam(value = "apply_unit",required = false) String apply_unit,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum
    ){
        return smsUseMoneyApplicationService.selectAllUseMoneyApply(apply_unit,
                pageSize,pageNum);
    }
    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportUseMoneyApplication(@RequestBody SmsUseMoneyApplicationExportVO exportVO,
                                                HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsUseMoneyApplicationService.exportUseMoneyApplication(exportVO, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
}

